package top.javarem.medical.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.javarem.medical.common.response.Response;
import top.javarem.medical.domain.dto.MedicalKnowledgeDTO;
import top.javarem.medical.domain.po.Knowledge;
import top.javarem.medical.service.KnowledgeService;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/v1/medical/knowledge")
@CrossOrigin("*")
public class MedicalKnowledgeController {

    @Resource
    private KnowledgeService knowledgeService;

    //根据分类ID获取医疗知识列表
    @GetMapping("/show/{categoryId}")
    public Response<List<MedicalKnowledgeDTO>> getMedicalKnowledge(@PathVariable Integer categoryId) {
        if (categoryId == null) {
            return Response.error();
        }
        List<MedicalKnowledgeDTO> medicalKnowledgeDTOList = knowledgeService.queryByCategoryId(categoryId);
        return Response.success(medicalKnowledgeDTOList);
    }

    /**
     * 后台管理接口
     */
    //分页查询
    @GetMapping("/page")
    public Response<List<MedicalKnowledgeDTO>> pageQuery(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String title,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<MedicalKnowledgeDTO> medicalKnowledgeDTOList = knowledgeService.pageQuery(categoryId, title, pageNum, pageSize);
            return Response.success(medicalKnowledgeDTOList);
        } catch (Exception e) {
            log.error("分页查询知识失败", e);
            return Response.error();
        }
    }

    //根据知识ID获取详细信息
    @GetMapping("/detail/{id}")
    public Response<MedicalKnowledgeDTO> getDetail(@PathVariable Long id) {
        try {
            Knowledge knowledge = knowledgeService.getById(id);
            MedicalKnowledgeDTO medicalKnowledgeDTO = new MedicalKnowledgeDTO();
            BeanUtils.copyProperties(knowledge, medicalKnowledgeDTO);
            return Response.success(medicalKnowledgeDTO);
        } catch (Exception e) {
            log.error("获取知识详情失败", e);
            return Response.error();
        }
    }

    @PostMapping("/add")
    public Response<Boolean> addKnowledge(@RequestBody @Validated MedicalKnowledgeDTO dto) {
        try {
            return Response.success(knowledgeService.addKnowledge(dto));
        } catch (Exception e) {
            log.error("添加知识失败", e);
            return Response.error();
        }
    }

    @PutMapping("/update")
    public Response<Boolean> updateKnowledge(@RequestBody @Validated MedicalKnowledgeDTO dto) {
        try {
            return Response.success(knowledgeService.updateKnowledge(dto));
        } catch (Exception e) {
            log.error("更新知识失败", e);
            return Response.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public Response<Boolean> deleteKnowledge(@PathVariable Long id) {
        try {
            return Response.success(knowledgeService.deleteKnowledge(id));
        } catch (Exception e) {
            log.error("删除知识失败", e);
            return Response.error();
        }
    }

    @PostMapping("/batch/delete")
    public Response<Boolean> batchDelete(@RequestBody List<Long> ids) {
        try {
            return Response.success(knowledgeService.removeBatchByIds(ids));
        } catch (Exception e) {
            log.error("批量删除知识失败", e);
            return Response.error();
        }
    }


}
